<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/9/20
 * Time: 14:36
 * 快递鸟
 */

namespace app\common\library;

class Kdniao {
	public $uid;
	public $key;
	public $apiUlr;
	
	public function __construct(){
		$config = config('kdniao');
		$this->uid = $config['uid'];
		$this->key = $config['key'];
		$this->apiUlr = $config['apiUrl'];
	}
	
	
	/**
	 * 即时查询
	 * @param $shippingCode 快递公司编码
	 * @param $shippingNo 快递单号
	 * @return mixed
	 */
	public function getOrderTracesByJson($shippingCode, $shippingNo){
		
		$requestData= "{'OrderCode':'','ShipperCode':'$shippingCode','LogisticCode':'$shippingNo'}";
		
		$datas = array(
			'EBusinessID' => $this->uid,
			'RequestType' => '1002',
			'RequestData' => urlencode($requestData) ,
			'DataType' => '2',
		);
		$datas['DataSign'] = $this->encrypt($requestData);
		
		$url = 'http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx';
		$result = curl_get_info($url, 'post', $datas);
		return $result;
	}
	
	/**
	 * 电商Sign签名生成
	 * @param data 内容
	 * @param appkey Appkey
	 * @return DataSign签名
	 */
	private function encrypt($data) {
		return urlencode(base64_encode(md5($data.$this->key)));
	}
}
